/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.FileUtil;
/*     */ import java.io.BufferedInputStream;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.PrintStream;
/*     */ import java.util.Arrays;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestFileUtil
/*     */   extends TestCase
/*     */ {
/*     */   private File TEST_DIR;
/*     */   
/*     */   private File genFile(String path, int numBytes)
/*     */     throws Exception
/*     */   {
/*  40 */     File file = new File(path);
/*  41 */     FileOutputStream fos = new FileOutputStream(file);
/*     */     
/*  43 */     byte[] bytes = new byte[numBytes];
/*  44 */     int offset = 0;
/*  45 */     for (int i = 0; i < numBytes; i++) {
/*  46 */       byte[] src = (String.valueOf(i) + "\n").getBytes();
/*  47 */       if (offset + src.length < numBytes) {
/*  48 */         System.arraycopy(src, 0, bytes, offset, src.length);
/*  49 */         offset += src.length;
/*     */       }
/*     */       else {
/*  52 */         while (offset < numBytes) {
/*  53 */           bytes[(offset++)] = 42;
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*  58 */     ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
/*  59 */     ByteUtil.copy(bais, true, fos, true);
/*  60 */     return file;
/*     */   }
/*     */   
/*     */   private void printFile(File file) throws Exception {
/*  64 */     InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
/*  65 */     BufferedReader reader = new BufferedReader(isr);
/*     */     String line;
/*  67 */     while ((line = reader.readLine()) != null) {
/*  68 */       System.out.println(line);
/*     */     }
/*     */   }
/*     */   
/*     */   private boolean isGzip(File file) throws Exception {
/*  73 */     InputStream is = new BufferedInputStream(new FileInputStream(file));
/*  74 */     int header = is.read() | is.read() << 8;
/*  75 */     is.close();
/*  76 */     if (header == 35615) {
/*  77 */       return true;
/*     */     }
/*  79 */     return false;
/*     */   }
/*     */   
/*     */   protected void setUp() throws Exception {
/*  83 */     String tempdir = System.getProperty("java.io.tmpdir");
/*  84 */     this.TEST_DIR = new File(tempdir + "/" + "testFileUtil");
/*  85 */     FileUtil.ensureDirExists(this.TEST_DIR);
/*     */   }
/*     */   
/*     */   protected void tearDown() throws Exception {
/*  89 */     FileUtil.deleteDir(this.TEST_DIR);
/*     */   }
/*     */   
/*     */   private File newFile(String fileName) {
/*  93 */     return new File(newFileName(fileName));
/*     */   }
/*     */   
/*     */   private String newFileName(String fileName) {
/*  97 */     return this.TEST_DIR.getAbsolutePath() + "/" + fileName;
/*     */   }
/*     */   
/*     */   public void testCompress() throws Exception {
/* 101 */     File orig = genFile(newFileName("junk.txt"), 1024);
/*     */     
/*     */ 
/*     */ 
/* 105 */     File compressed = newFile("junk.compressed");
/* 106 */     FileUtil.compress(orig, compressed, true);
/* 107 */     assertTrue(isGzip(compressed));
/*     */     
/*     */ 
/* 110 */     File uncompressed = newFile("junk.uncompressed");
/* 111 */     FileUtil.uncompress(compressed, uncompressed, true);
/*     */     
/*     */ 
/* 114 */     byte[] origBytes = ByteUtil.getContent(orig);
/* 115 */     byte[] uncompressedBytes = ByteUtil.getContent(uncompressed);
/* 116 */     assertTrue(Arrays.equals(origBytes, uncompressedBytes));
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 121 */     TestUtil.cliSetup();
/* 122 */     TestUtil.runTest(TestFileUtil.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestFileUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */